package com.dextrys.testing.seleniumdemo.factory;

import java.util.Hashtable;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import com.dextrys.testing.seleniumdemo.jxl.IExcelAccess;
import com.dextrys.testing.seleniumdemo.jxl.impl.ExcelAccessImpl;
import com.dextrys.testing.seleniumdemo.util.Config;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

public class TestCaseFactory extends SeleneseTestCase {

	public IExcelAccess ea;
	public Selenium selenium;
	public TestCaseFactory tcf;

	public void generateTestCase(Workbook caseBook, String dataName) {
		Sheet stepsSheet = caseBook.getSheet("Steps");
		Sheet objSheet = caseBook.getSheet("Object");
		Sheet dataSheet = caseBook.getSheet(dataName);
		
		Hashtable dataHash = getTestData(dataSheet);
		Hashtable objHash = getTestObj(objSheet);
		
		boolean isTrue = true;
		tcf = new TestCaseFactory();
		selenium = SeleniumFactory.createSelenium(Config.FIREFOX, Config.URL);

		selenium.start();
		selenium.open("");
		
		for (int i = 1; i < stepsSheet.getRows(); i++) {
			String action = stepsSheet.getCell(0, i).getContents();
			String widget = stepsSheet.getCell(1, i).getContents();
			String dataVar = stepsSheet.getCell(2, i).getContents();
			String dataVal = dataHash.get(dataVar).toString();
			
			String locator = null;
			if (widget != null && !widget.equals("")) {
				Hashtable obj = (Hashtable)objHash.get(widget);
				String path = obj.get("XPath").toString();
				locator = tcf.convertPath(path);
			}
			
			isTrue = isTrue
					&& tcf.executeSelenium(selenium, action, locator, dataVal);
		}
		if (isTrue) {
			System.out.println("pass");
		} else {
			System.out.println("fail");
		}
		// selenium.close();
	}

	private Hashtable getTestData(Sheet dataSheet) {
		Hashtable mydate = new Hashtable();
		for (int i = 1; i < dataSheet.getRows(); i++) {
			String key = dataSheet.getCell(0, i).getContents();
			String val = dataSheet.getCell(1, i).getContents();
			mydate.put(key, val);
		}
		return mydate;
	}

	private Hashtable getTestObj(Sheet objSheet) {
		Hashtable mydate = new Hashtable();
		for (int i = 1; i < objSheet.getRows(); i++) {
			String key = objSheet.getCell(0, i).getContents();
			Hashtable myItem = new Hashtable();
			mydate.put(key, myItem);
			
			myItem.put("Type", objSheet.getCell(1, i).getContents());
			myItem.put("Page", objSheet.getCell(2, i).getContents());
			myItem.put("XPath", objSheet.getCell(3, i).getContents());
		}
		return mydate;
	}

	// Set element path
	public String convertPath(String path) {
		String locator = "xpath=//" + path;
		return locator;
	}

	// Execute selenium language
	public boolean executeSelenium(Selenium selenium, String action,
			String locator, String data) {
		try {
			if (action.equals("type")) {
				selenium.type(locator, data);
			} else if (action.equals("click")) {
				selenium.click(locator);
			} else if (action.equals("clickAndWait")) {
				selenium.waitForPageToLoad("100000");
			} else if (action.equals("assertTitle")) {
				assertEquals(data, selenium.getTitle());
			} 
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
